<?php
defined('IN_MAIN') or exit('Invalid Access!');

function format_page_admin($in_page, $in_total_page, $in_file, $page_name=""){
	$max_page = 20;
	$before_page_no = 10;
	$page_name=(!$page_name? "nav" : $page_name);
	$file=$in_file? $in_file : basename($_SERVER["PHP_SELF"]);
	if($in_total_page>1){
		$get=$_GET;
		foreach($get as $field => $value){
			if($field!=$page_name){
				$query.="&$field=".urlencode($value);
				$inputfields .= "<input type='hidden' name=\"$field\" value=\"$value\" />";
			}
		}
		if($in_total_page > $max_page){
			$start_page = ($start = ($in_page - $before_page_no)) < 1? 1 : $start;
			if($start_page + $max_page > $in_total_page){
				$start_page = $in_total_page - $max_page + 1;
			}
			$total_page = $start_page + $max_page - 1;
		}else{
			$start_page = 1;
			$total_page = $in_total_page;
		}
		for($i=$start_page;$i<=$total_page;$i++){
			if($in_page!=$i){
				$nav_link.="<a href='$file?$query&$page_name=$i'>$i</a> ";
			}else{
				$nav_link.="$i ";
			}
		}
		if($start_page != 1){
			$nav_link = "<a href='$file?$query&$page_name=1'>1</a> $nav_link";
		}
		if($total_page != $in_total_page){
			$nav_link .= "<a href='$file?$query&$page_name=$in_total_page'>$in_total_page</a> ";
		}
		$prev_no=$in_page-1;
		$prev=($prev_no>=1? "<a href='$file?$query&$page_name=$prev_no'>Previous</a> " : "");
		$next_no=$in_page+1;
		$next=($next_no<=$in_total_page? "<a href='$file?$query&$page_name=$next_no'>Next</a> " : "");
		
		if($in_total_page > $max_page){
			$go_to_page = 
			"<form name='_paging_form' method='get' action='$file'>
			$inputfields
			".__("Go to page").": <input type='text' name='$page_name' style='width:55px;' /> <input type='submit' value=\"".__("Go")."\" />
			</form>";
		}
		$nav_link="$prev $nav_link $next $go_to_page";
	}
	return $nav_link;
}

function format_page_admin_aj($in_page, $in_total_page, $in_file, $page_name=""){
	$max_page = 20;
	$before_page_no = 10;
	$page_name=(!$page_name? "nav" : $page_name);
	$file=$in_file? $in_file : basename($_SERVER["PHP_SELF"]);
	if($in_total_page>1){
		$get=$_GET;
		foreach($get as $field => $value){
			if($field!="nav"){
				$query.="&$field=".urlencode($value);
			}
		}
		if($in_total_page > $max_page){
			$start_page = ($start = ($in_page - $before_page_no)) < 1? 1 : $start;
			if($start_page + $max_page > $in_total_page){
				$start_page = $in_total_page - $max_page + 1;
			}
			$total_page = $start_page + $max_page - 1;
		}else{
			$start_page = 1;
			$total_page = $in_total_page;
		}
		for($i=$start_page;$i<=$total_page;$i++){
			$nav_link.="<a class='".($in_page==$i? "nav_page_selected " : "")."nav' href='$page_name=$i'>$i</a> ";
		}
		if($start_page != 1){
			$nav_link = "<a class='nav' href='$page_name=1'>1</a> $nav_link";
		}
		if($total_page != $in_total_page){
			$nav_link .= "<a class='nav' href='$page_name=$in_total_page'>$in_total_page</a> ";
		}
		$prev_no=$in_page-1;
		$prev=($prev_no>=1? "<a class='nav' href='$page_name=$prev_no'>Previous</a> " : "");
		$next_no=$in_page+1;
		$next=($next_no<=$in_total_page? "<a class='nav' href='$page_name=$next_no'>Next</a> " : "");
		
		if($in_total_page > $max_page){
			$go_to_page = 
			"<form name='_paging_form' method='get' action='$file'>
			".__("Go to page").": <input type='text' name='$page_name' style='width:55px;' id='$page_name' /> <input type='submit' value=\"".__("Go")."\" />
			</form>";
		}
		$nav_link="<span class='nav_page'>$prev $nav_link $next $go_to_page</span>";
	}
	return $nav_link;
}

function format_page_aj($in_page, $in_total_page, $in_total_slot, $in_page_name=""){
 //setting for page name
 $page_name=(!$in_page_name? "nav" : $in_page_name);
 $min_slot=6;

 if($in_total_page>1){
  //navigation button
  $prev_no=$in_page-1;
  if($prev_no>=1){
   $prev="<div class='nav_nav nav_page'><a href='nav=$prev_no'><</a></div>";
   $first="<div class='nav_nav nav_page'><a href='nav=1'>&laquo;</a></div>";
  }
  $next_no=$in_page+1;
  $next=($next_no<=$in_total_page? "<div class='nav_nav nav_page'><a href='nav=$next_no'>></a></div>" : "");
  $last=($next_no<=$in_total_page? "<div class='nav_nav nav_page'><a href='nav=$in_total_page'>&raquo;</a></div>" : "");

  //navigation numbering
  $slot_used=($prev? 1 : 0)+($first? 1 : 0)+($next? 1 : 0)+($last? 1 : 0);
  $total_slot=($in_total_slot<$min_slot? $min_slot : $in_total_slot);
  $slot_left=$total_slot-$slot_used;
  if($in_total_page>$slot_left){                 
   $slot_left_for_number=$slot_left-1;//1 slot for "..."
   $slot_left_for_left_right=$slot_left_for_number-1;//1 slot for the selected page
   if($slot_left_for_left_right>0){
    $slot_for_left=$slot_for_right=$slot_left_for_left_right/2;
    $slot_for_left=Ceil($slot_for_left);
    $slot_for_right=floor($slot_for_right);
    if($in_page-1<$slot_for_left){//number of left site numbering is less than the left site slot reserved, add the overflow to right site
     $slot_for_left_overflow=$slot_for_left-($in_page-1);
     $slot_for_left=$in_page-1;
     $slot_for_right+=$slot_for_left_overflow;
    }
    if($in_total_page-$in_page<$slot_for_right){//right site numbering overflow (no > total page)
     $slot_for_right_overflow=$slot_for_right-($in_total_page-$in_page);
     $slot_for_right=$in_total_page-$in_page;
     $slot_for_left+=$slot_for_right_overflow;
    }
    //check for need more '...'
    if(!$no_need_more){
     if($slot_for_right+$in_page+1>=$in_total_page){//check to see if right site number is enough to display all the remaining digits, +1 as 1 slot for '...'
      $no_need_more=1;
      if($slot_for_right+$in_page>=$in_total_page){
       $slot_for_left+=1;
      }else{
       $slot_for_right+=1;
      }
     }
    }

    //format left & right
    for($i=($in_page-$slot_for_left);$i<$in_page;$i++){
     $left_nav_link.="<div class='nav_no nav_page'><a href='nav=$i'>$i</a></div>";
    }
    for($i=($in_page+1);$i<=($in_page+$slot_for_right);$i++){
     $right_nav_link.="<div class='nav_no nav_page'><a href='nav=$i'>$i</a></div>";
    }
   }
   $more_nav_link=(!$no_need_more? "<div class='nav_no_more'>...</div>" : "");
   $selected_page_nav="<div class='nav_no_selected nav_page'><a href='nav=$in_page'>$in_page</a></div>";
   $nav_no=$left_nav_link.$selected_page_nav.$right_nav_link.$more_nav_link;
  }else{
   for($i=1;$i<=$in_total_page;$i++){
    $nav_no.="<div class='".($in_page==$i? "nav_no_selected" : "nav_no")." nav_page'><a href='nav=$i'>$i</a></div>";
   }
  }

  $clear="<div style='clear:both;'></div>";
  $nav_link="<div class='nav_link'>".$first.$prev.$nav_no.$next.$last."</div>$clear";
 }
 return $nav_link;
}

function format_page($in_page, $in_total_page, $in_total_slot, $in_page_name=""){
 //setting for page name, file, querystring
 $page_name=!$in_page_name? "nav" : $in_page_name;
 $file=!$in_file? basename($_SERVER["PHP_SELF"]) : $in_file;
 $querystring=!$in_querystring? $_SERVER["QUERY_STRING"] : $in_querystring;
 $min_slot=6;

 if($in_total_page>1){
  //remove page name from querystring, if any
  $query=preg_match("/^$page_name/", $querystring)? preg_replace("/$page_name=\d*&*/", '',  $querystring) : preg_replace("/&*$page_name=\d*/", '', $querystring);
  $query=preg_replace('/^&/', '', $query);

  //navigation button
  $prev_no=$in_page-1;
  if($prev_no>=1){
   $prev="<div class='nav_nav'><a href=\"$file?".($query? "$query&" : "")."$page_name=$prev_no\"><</a></div>";
   $first="<div class='nav_nav'><a href=\"$file?".($query? "$query&" : "")."$page_name=1\">&laquo;</a></div>";
  }
  $next_no=$in_page+1;
  $next=($next_no<=$in_total_page? "<div class='nav_nav'><a href=\"$file?".($query? "$query&" : "")."$page_name=$next_no\">></a></div>" : "");
  $last=($next_no<=$in_total_page? "<div class='nav_nav'><a href=\"$file?".($query? "$query&" : "")."$page_name=$in_total_page\">&raquo;</a></div>" : "");

  //navigation numbering
  $slot_used=($prev? 1 : 0)+($first? 1 : 0)+($next? 1 : 0)+($last? 1 : 0);
  $total_slot=($in_total_slot<$min_slot? $min_slot : $in_total_slot);
  $slot_left=$total_slot-$slot_used;
  if($in_total_page>$slot_left){                 
   $slot_left_for_number=$slot_left-1;//1 slot for "..."
   $slot_left_for_left_right=$slot_left_for_number-1;//1 slot for the selected page
   if($slot_left_for_left_right>0){
    $slot_for_left=$slot_for_right=$slot_left_for_left_right/2;
    $slot_for_left=Ceil($slot_for_left);
    $slot_for_right=floor($slot_for_right);
    if($in_page-1<$slot_for_left){//number of left site numbering is less than the left site slot reserved, add the overflow to right site
     $slot_for_left_overflow=$slot_for_left-($in_page-1);
     $slot_for_left=$in_page-1;
     $slot_for_right+=$slot_for_left_overflow;
    }
    if($in_total_page-$in_page<$slot_for_right){//right site numbering overflow (no > total page)
     $slot_for_right_overflow=$slot_for_right-($in_total_page-$in_page);
     $slot_for_right=$in_total_page-$in_page;
     $slot_for_left+=$slot_for_right_overflow;
    }
    //check for need more '...'
    if(!$no_need_more){
     if($slot_for_right+$in_page+1>=$in_total_page){//check to see if right site number is enough to display all the remaining digits, +1 as 1 slot for '...'
      $no_need_more=1;
      if($slot_for_right+$in_page>=$in_total_page){
       $slot_for_left+=1;
      }else{
       $slot_for_right+=1;
      }
     }
    }

    //format left & right
    for($i=($in_page-$slot_for_left);$i<$in_page;$i++){
     $left_nav_link.="<div class='nav_no'><a href=\"$file?".($query? "$query&" : "")."$page_name=$i\">$i</a></div>";
    }
    for($i=($in_page+1);$i<=($in_page+$slot_for_right);$i++){
     $right_nav_link.="<div class='nav_no'><a href=\"$file?".($query? "$query&" : "")."$page_name=$i\">$i</a></div>";
    }
   }
   $more_nav_link=(!$no_need_more? "<div class='nav_no_more'>...</div>" : "");
   $selected_page_nav="<div class='nav_no_selected'><a href=\"$file?".($query? "$query&" : "")."$page_name=$in_page\">$in_page</a></div>";
   $nav_no=$left_nav_link.$selected_page_nav.$right_nav_link.$more_nav_link;
  }else{
   for($i=1;$i<=$in_total_page;$i++){
    $nav_no.="<div class='".($in_page==$i? "nav_no_selected" : "nav_no")."'><a href=\"$file?".($query? "$query&" : "")."$page_name=$i\">$i</a></div>";
   }
  }

  $clear="<div style=\"clear:both;\"></div>";
  $nav_link="<div class='nav_link'>".$first.$prev.$nav_no.$next.$last."</div>$clear";
 }
 return $nav_link;
}
?>